<template>
	<view class="home">
		<view class="home_header">
			<view class="home_avater">
				<u-avatar class="home_avater_a" :src="src" size="60"></u-avatar>
			</view>
			<view class="home_name" @click="toBusiness">
				<view style="">{{businessData.name}}欢迎您！</view>
				<view style="font-weight: bold;">CHENGSHISHITANG</view>
			</view>
		</view>
		
		<view class="home-content">
			<view class="conten-item">
				<u-icon name="calendar" color="#E3CCA1" size="24"></u-icon>
				<text style="margin-left: 15rpx;font-weight: bold;font-size: 37rpx;">{{businessData.area}}</text>
			</view>
			<view class="conten-item">
				<u-icon name="map" color="#E3CCA1" size="18"></u-icon>
				<text style="margin-left: 22rpx;font-size: 25rpx;color: #bfbfbf;">{{businessData.position}}17-27号</text>
			</view>
			<view class="conten-item">
				<u-icon name="clock" color="#E3CCA1" size="15"></u-icon>
					早{{ businessData.beginTime }} ~ 晚{{businessData.endTime}}</text>
			</view>
		</view>
		
		<view class="home-scan">
			<view class="advance_choose">
				<image style="width: 150rpx;height: 150rpx;" src="../../static/home/hot.png"></image>
				<view style="font-weight: bold;font-size: 45rpx;">个人信息</view>
				<view style="font-size: 30rpx;color: #bfbfbf;">去修改个人信息</view>
			</view>
			<view class="scan_choose" @click="toScanning">
				<image style="width: 150rpx;height: 150rpx;" src="../../static/home/scan.png"></image>
				<view  style="font-weight: bold;font-size: 45rpx;">扫码点餐</view>
				<view  style="font-size: 30rpx;color: #bfbfbf;">扫餐桌二维码</view>
			</view>
		</view>
	</view>
</template>

<script>
import { LOGIN, BusinessDetail, byUserId_OrderDetail, byUserIdMessage } from '@/common/http.api.js'
export default {
	data() {
		return {
			src: '/static/home/avater.jpg',
			businessData:{}
		}
	},
	onLoad() {
		BusinessDetail().then( res => {
			this.businessData = res.rows[0]
			// this.swipers = this.businessData.nearImage.split(",")
			// console.log(this.swipers)
		})
	},
	methods: {
		toScanning(){
			if (this.$store.state.userId) {
				byUserId_OrderDetail(this.$store.state.userId).then( respo => {
					for (let i=0; i<respo.rows.length; i++) {
						if (respo.rows[i].currentStatus == '支付失败') {
							uni.showModal({
								title: '',
								content: '您有支付失败订单，请支付',
								confirmText: '去支付',
								success: respon => {
									if (respon.confirm) {
										uni.navigateTo({
											url: `/pagesA/home/closeAccount/closeAccount?orderId=${respo.rows[i].id}`
										})
									}
								}
							})
							return
						}
					}
					uni.navigateTo({
						url: '/pagesA/home/scanning/scanning'
					}) 
				})
			} else {
				uni.getUserProfile({
					desc: "获取信息用于登录",
					success: (userProfileRes) => {
						console.log(userProfileRes, 'userProfileRes')
						uni.login({
							success: (loginRes) => {
								// console.log(loginRes);
								const code = loginRes.code
								const secret = '66f401760c3b9b9e6d4ab320fc301785'
								const appid = 'wx2b7223dc574f8ec6'
								// 这个接口用于校验code
								const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${code}&grant_type=authorization_code`
								uni.request({
									url: url,
									success: (res) => {
										// console.log(res, 'res')
										const user = {
											// 这里要的userInfo，因为用户没登录，this.userInfo的值是{}
											openid: res.data.openid,
											nickName: userProfileRes.userInfo.nickName,
											avatar: userProfileRes.userInfo.avatarUrl,
											sex: userProfileRes.userInfo.gender
										}
										// console.log(user, "user");
										// 拿到openid之后 通过登录接口 登录 把个人信息传给接口
										LOGIN(user).then( resp => {
											// console.log(resp, 'resp')
											// 从返回数据里拿到userId 并存到仓库和缓存
											this.$store.commit('SET_USERID', resp.data)
											uni.setStorageSync('scan-userId', resp.data)
											// // 从返回数据里拿到token 并存到仓库和缓存
											this.$store.commit('SET_TOKEN', resp.msg)
											uni.setStorageSync('scan-token', resp.msg)
											
											//获取个人信息
											byUserIdMessage( resp.data ).then( response => {
												this.$store.commit('SET_USERINFO', response.data)
												uni.setStorageSync('scan-userinfo', JSON.stringify(response.data))
											})
											byUserId_OrderDetail(resp.data).then( respo => {
												for (let i=0; i<respo.rows.length; i++) {
													if (respo.rows[i].currentStatus == '支付失败') {
														uni.showModal({
															title: '',
															content: '您有支付失败订单，请支付',
															confirmText: '去支付',
															success: respon => {
																if (respon.confirm) {
																	uni.navigateTo({
																		url: `/pagesA/home/closeAccount/closeAccount?orderId=${respo.rows[i].id}`
																	})
																}
															}
														})
														return
													}
												}
												uni.navigateTo({
													url: '/pagesA/home/scanning/scanning'
												}) 
											})
										})
									}
								})
							}
						})
					},
					fail: () => {
						uni.showToast({
							icon: "none",
							title: "已拒绝授权"
						})
					}
				})
			}
		},
		
		toBusiness(){
			uni.navigateTo({
				url: '/pagesB/home/business/business'
			})
		}
	}
}
</script>

<style lang="less" scoped>
body{
	background-color: rgb(247,247,247);
}
.home{
	.home_header{
		margin: 35rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		.home_name{
			flex-grow: 1;
			margin: 25rpx;
			line-height: 50rpx;
		}
	}
	
	.home-content{
		width: 85%;
		margin: 30rpx;
		padding: 20rpx;
		border-radius: 20rpx;
		background-color: white;
		.conten-item{
			display: flex;
			margin: 15rpx;
		}
	}
	
	.home-scan{
		width: 85%;
		margin: 30rpx;
		padding: 25rpx;
		border-radius: 20rpx;
		background-color: white;
		display: flex;
		justify-content: center;
		.advance_choose{
			padding-right: 100rpx;
			border-right: 1px solid #e6e6e6;
			text-align: center;
			line-height: 60rpx;
		}
		.scan_choose{
			padding-left: 100rpx;
			text-align: center;
			line-height: 60rpx;
		}
	}
}
</style>
